package com.cndw.kungfu.business;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import com.cndw.kungfu.comm.LocalCommand;
import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.event.IntelligenceOptimizeHandler;
import com.cndw.kungfu.event.RobotHandler;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.NumberProcesser;
import com.cndw.kungfu.ext.PropertiesLib;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.sys.ChatMain;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.annotation.UnAuth;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

@Controller
public class LocalhostTest {
	private static long nowId = 0;
	private static ChatRebot chatRebot;
	private static int scondInterval = 0;
	private static AtomicInteger debug = new AtomicInteger();
	@UnAuth
	@LocalCommand
	public ResponseMessage test(Session session,String sign){
		List<String> ads = ConstantsConfig.getAdWorld();
		Map<String, Integer> adsNums = new HashMap<String, Integer>();
		for (String e : ads) {
			int old = 0;
			if (adsNums.containsKey(e)) {
				old = adsNums.get(e);
			}
			old++;
			adsNums.put(e, old);
		}
		List<String> oneAds = new ArrayList<String>();
		List<String> threeAds = new ArrayList<String>();
		Collections.sort(oneAds);
		Collections.sort(threeAds);
		Iterator<Entry<String, Integer>> a= adsNums.entrySet().iterator();
		while (a.hasNext()) {
			Map.Entry<java.lang.String, java.lang.Integer> entry = (Map.Entry<java.lang.String, java.lang.Integer>) a.next();
			if (entry.getValue() >= 3) {
				threeAds.add(entry.getKey());
			}else {
				oneAds.add(entry.getKey());
			}			
		}
		StringBuilder sb = new StringBuilder();
		for (String string : threeAds) {
			sb.append(string).append(",");
		}
		System.err.println(sb.toString());

		StringBuilder sb2 = new StringBuilder();
		for (String string : oneAds) {
			sb2.append(string).append(",");
		}
		System.err.println(sb2.toString());
		
		LogUtil.getMain().debug(Context.getBusinessServer().getOnLine(1800));
		
		return ResponseMessage.getSuccMessage(true);
	}

	@UnAuth
	@LocalCommand
	public ResponseMessage robot(Session session, String sign, int sceneId, int num) {
		if (!ConstantsConfig.isLocalHost() || num >= 0) {
			return null;
		}
		IntelligenceOptimizeHandler.setOpenOpitimize(false);
		int errnum = 0;
		int sucNum = 0;
		for (int i = 0; i < num; i++) {

			if (nowId == 1193001) {
				nowId = 2147483648001L;
			} else if (nowId > 1193001) {
				nowId += 1000;
			} else {
				nowId += 1;
			}
			RobotHandler r = new RobotHandler(sceneId, nowId);
			if (!r.isSuc()) {
				num++;
				errnum++;
				if (errnum > 1100) {
					break;
				}
				continue;
			}
			sucNum++;
			Context.getTimerServer().scheduleAtFixedRate(r, NumberProcesser.mtRand(10) + i, NumberProcesser.mtRand(5));

		}

		return ResponseMessage.getSuccMessage(sucNum);

	}

	@LocalCommand
	public ResponseMessage chatRebot(Session session, String sign, int nums) {
		IntelligenceOptimizeHandler.setOpenOpitimize(false);
		if (nums == 0) {
			scondInterval = 1000;
		} else {
			scondInterval = 1000 / nums;
		}
		if (chatRebot == null) {
			synchronized (LocalhostTest.class) {
				if (chatRebot == null) {
					chatRebot = new ChatRebot();
					Context.getTimerServer().execute(chatRebot);
				}
			}
		}

		return ResponseMessage.getSuccMessage(true);
	}

	class ChatRebot implements Runnable {
		private List<String> messageList = new ArrayList<String>();
		private int messageNums = 0;

		public ChatRebot() {
			Properties temp = PropertiesLib.load("language");
			Collection<Object> messages = temp.values();
			for (Object dat : messages) {
				messageList.add(dat.toString());
			}
			messageNums = messages.size() - 1;
		}

		@Override
		public void run() {

			while (true) {

				for (Player player : Context.getCacheCenter().getPlayers()) {

					String message = "123456";
					if (messageNums > 0) {
						message = messageList.get(NumberProcesser.mtRand(messageNums));
					}
					message += debug.incrementAndGet();

					ChatMain.sendMessage(1, message, player);

					try {
						TimeUnit.MILLISECONDS.sleep(scondInterval);
						// System.out.println(scondInterval);

					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}

			}

		}
	}

}
